package com.qf.springboot.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qf.springboot.permission.entity.Pers;
import com.qf.springboot.permission.entity.Role;
import com.qf.springboot.permission.entity.RolePerms;
import com.qf.springboot.permission.entity.UserRole;
import com.qf.springboot.permission.service.PersService;
import com.qf.springboot.permission.service.RolePermsService;
import com.qf.springboot.permission.service.RoleService;
import com.qf.springboot.permission.service.UserRoleService;
import com.qf.springboot.user.entity.User;
import com.qf.springboot.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;

/**
 * 控制器
 */
@Controller
@RequestMapping("/user")
@Api(tags = "用户管理模块",description = "这个模块包含了用户管理相关的接口")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    UserRoleService userRoleService;

    @Autowired
    RoleService roleService;

    @Autowired
    RolePermsService rolePermsService;

    @Autowired
    PersService persService;

    @ApiOperation(value = "这是一个测试方法",notes = "该方法接收post请求,不用传参数")
    @RequestMapping (value = "/register",method = RequestMethod.POST )
    public String regist(User user) {
        userService.regist(user);

        //注册成功重定向到登录页面
        return "redirect:/login.html";
    }

    @ApiOperation(value = "这是一个测试方法",notes = "该方法接收post请求,不用传参数")
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(String username, String password, boolean rememberMe) {

        Subject subject = SecurityUtils.getSubject();

        //设置免登录信息---->rememberMe 给usernamePasswordToken
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password,rememberMe);
        subject.login(usernamePasswordToken);
        //获取user对象
        User user =(User) subject.getPrincipal();
        //在UserRole获取该user对象对应的Roleid
        LambdaQueryWrapper<UserRole> URWapper = new LambdaQueryWrapper<UserRole>()
                .select(UserRole::getRoleid)
                .eq(UserRole::getUserid, user.getId());
        List<Object> roleIdList = userRoleService.listObjs(URWapper);

        //查user对应的角色名
        LambdaQueryWrapper<Role> RWapper = new LambdaQueryWrapper<Role>()
                .select(Role::getName)
                .in(Role::getId, roleIdList);
        List<Object> roleNameList = roleService.listObjs(RWapper);
        ArrayList<String> roleNameStringList = new ArrayList<String>();
        for (Object o : roleNameList) {
            roleNameStringList.add(o.toString());
        }
        //把角色列表给user的role
        user.setRole(roleNameStringList);

        //查user对应的权限名
        LambdaQueryWrapper<RolePerms> RPWapper = new LambdaQueryWrapper<RolePerms>()
                .select(RolePerms::getPermsid)
                .in(RolePerms::getRoleid, roleIdList);
        List<Object> permIDList = rolePermsService.listObjs(RPWapper);

        LambdaQueryWrapper<Pers> PWapper = new LambdaQueryWrapper<Pers>()
                .select(Pers::getName)
                .in(Pers::getId, permIDList);
        List<Object> permNameList = persService.listObjs(PWapper);
        ArrayList<String> permNameStringList = new ArrayList<>();
        for (Object o : permNameList) {
            permNameStringList.add(o.toString());
        }
        //把perm给user
        user.setPerms(permNameStringList);


        return "redirect:/home.html";
    }

    @ApiOperation(value = "这是一个测试方法",notes = "该方法接收get请求,不用传参数")
    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public String logout() {
        //获取当前用户对象
        Subject subject = SecurityUtils.getSubject();

        //注销当前用户
        subject.logout();

        //重定向到登录页面
        return "redirect:/login.html";
    }
}

